package models;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import views.SurveyResultsView;
import views.TrueFalseQView;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import controllers.ControllerMonitor;
import controllers.SurveyResultsController;
import controllers.TrueFalseQController;

import answers.Response;

import main.ConsoleInterpreterSingleton;
import mvc.AbstractModel;
import mvc.AbstractView;

/*
 * class SurveyResultsModel
 * Stores all responses of a taken survey
 * name - filename
 *  
 */
public class SurveyResultsModel extends AbstractModel
{	
	public SurveyResultsModel(String firstName, String lastName, String surveyFilename, String resultFilename)
	{
		super(resultFilename, "");
		setFirstName(firstName);
		setLastName(lastName);
		setSurveyFilename(surveyFilename);
		setStatus(new String("ungraded"));
		monitor = new ControllerMonitor();
		itsResponse = new LinkedList<Response>();
		setGradeMode(false);
	}
	
	public void addResponse(Response aResponse)
	{
		itsResponse.add(aResponse);
	}
	
	public void removeResponse(int questionPosition)
	{
		boolean hasResponse = false;
		
		Iterator<Response> iter = itsResponse.iterator();
		int i = 0;
		while(iter.hasNext())
		{
			Response r = iter.next();
			if(r.getQuestionPosition() == questionPosition)
			{
				hasResponse = true;
				break;
			}
			++i;
		}
		
		if(hasResponse)
		{
			itsResponse.remove(i);
		}
	}

	public static void save(SurveyResultsModel results)
	{
		results.cleanup();
		FileOutputStream out;
		try
		{
			out = new FileOutputStream(results.getName());
			xstream.toXML(results, out);
			out.close();
		} 
		catch (FileNotFoundException e)
		{
			System.out.println("File " + results.getName() + " not found \\ not legal");
			e.printStackTrace();
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	@Override
	public AbstractView createView()
	{
		if(itsIObservers.size() > 0)
		{
			return null;
			
		}
		SurveyResultsController controller = new SurveyResultsController(this);
		AbstractView view  = new SurveyResultsView(controller, this);
		this.attach(view);
		return view;
	}

	public void gradeSurvey()
	{
		// show the results
		notifyObservers();
		setGradeMode(true);
		setCaption("press ? for instructions on grading");
		notifyObservers();
	}
	
	public void gradeQuestion(int position, double score)
	{
		Response response = itsResponse.get(position - 1);
		response.setScore(score);
	}
	
	public void doneGrading()
	{
		AbstractView view = getControllerMonitorView();
		setStatus("graded");
		setCaption("Done grading! back to desktop view");
		notifyObservers();
		setGradeMode(false);
		SurveyResultsModel.save(this);
		ConsoleInterpreterSingleton interpreter = ConsoleInterpreterSingleton.getInstance();
		interpreter.pipeEventsTo(view);
	}
	
	
	public double getMaxScore()
	{
		double retval = 0;
	
		Iterator<Response> iter = itsResponse.iterator();
		while(iter.hasNext())
		{
			retval += iter.next().getPoints();
		}
		return retval;
	}
	
	public double getScore()
	{
		double retval = 0;
		
		Iterator<Response> iter = itsResponse.iterator();
		while(iter.hasNext())
		{
			retval += iter.next().getScore();
		}
		return retval;
	}
	
	public int getCorrect()
	{
		int correct = 0;
		
		Iterator<Response> iter = itsResponse.iterator();
		while(iter.hasNext())
		{
			if(iter.next().getScore() > 0)
			{
				correct++;
			}
		}
		return correct;
	}
	
	public void setControllerMonitorView(AbstractView view)
	{
		monitor.setView(view);
	}
	
	public AbstractView getControllerMonitorView()
	{
		return monitor.getView();
	}
	
	public int getWrong()
	{
		return itsResponse.size() - getCorrect();
	}
	
	public void setSurveyFilename(String surveyFilename)
	{
		this.surveyFilename = surveyFilename;
	}
	public String getSurveyFilename()
	{
		return surveyFilename;
	}

	public void setFirstName(String firstName)
	{
		this.firstName = firstName;
	}
	public String getFirstName()
	{
		return firstName;
	}

	public void setLastName(String lastName)
	{
		this.lastName = lastName;
	}
	public String getLastName()
	{
		return lastName;
	}
	public int size()
	{
		return itsResponse.size();
	}

	public void setStatus(String status)
	{
		this.status = status;
	}
	public String getStatus()
	{
		return status;
	}

	public void setGradeMode(boolean gradeMode)
	{
		this.gradeMode = gradeMode;
	}

	public boolean getGradeMode()
	{
		return gradeMode;
	}

	public List<Response> itsResponse;
	private String surveyFilename, firstName, lastName, status;
	private boolean gradeMode;
	private ControllerMonitor  monitor;
	private static XStream xstream = new XStream(new DomDriver());
}
